package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.workcellinfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.SecurityRiskRescMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.JsmWorkCellInfoMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.JsmWorkCellInstanceMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.workcellinfo.JsmIRiskInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作单元-风险配置service实现类
 * Created by jiangjx on 2016/12/29.
 */

public class JsmRiskInfoServiceImp implements JsmIRiskInfoService {

    /**
     * 风险配置接口
     */
    @Autowired
    private SecurityRiskRescMapper securityRiskRescMapper;

    @Autowired
    private JsmWorkCellInfoMapper jsmWorkCellInfoMapper;

    @Autowired
    private JsmWorkCellInstanceMapper jsmWorkCellInstanceMapper;

    /**
     * 新增一条风险配置
     * created by jiangjx on 2016年12月29日 10:49:48
     */
    @Override
    public Integer saveRiskInfo(SecurityRiskResc securityRiskResc, Employee employee) throws Exception {
        int returnVal;
        // Employee employee = employeeService.getLoginEmployee();
            //根据主键ID来区分是新增还是修改
            if (null == securityRiskResc.getSecurityRiskId()) {
                securityRiskResc.setSecurityRiskId(UniqueUtil.uuid());//set主键ID
                securityRiskResc.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));//set创建日期
                securityRiskResc.setCreatorId(employee.getEmployeeId());//set创建人ID
                securityRiskResc.setModifierId(employee.getEmployeeId());
                securityRiskResc.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                securityRiskResc.setIsdelete(Short.parseShort("0"));
                returnVal = securityRiskRescMapper.insertSelective(securityRiskResc); // 执行新增操作
            } else {
                securityRiskResc.setModifierId(employee.getEmployeeId());
                securityRiskResc.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                returnVal = securityRiskRescMapper.updateByPrimaryKeySelective(securityRiskResc);//执行修改操作
            }
            return returnVal;
    }

    /**
     * 分页加载风险配置列表信息
     * created by jiangjx on 2016年12月29日 15:26:50
     * @param securityRiskResc 不分页
     * @return
     */
    @Override
    public JSONObject toTable(SecurityRiskResc securityRiskResc) throws Exception {
        JSONObject jsonObject = new JSONObject();
        /* 进行分页查询分离 */
        List<SecurityRiskResc> rows = securityRiskRescMapper.queryTableList(securityRiskResc);//查询分页内容
        jsonObject.put("rows", JSON.toJSON(rows));
        return jsonObject;
    }

    /**
     * 删除风险配置信息
     * created by jiangjx on 2016年12月30日 09:18:17
     * @param riskid 风险id
     * @return
     */
    @Override
    public int delRiskInfo(String riskid) throws Exception {
        JSONArray riskids = JSONObject.parseArray(riskid);
        Map<String, JSONArray> map = new HashMap<>();
        map.put("riskids", riskids);
        int rownumb = securityRiskRescMapper.updateBySecurityRiskIds(map);//逻辑删除风险配置信息
        if (rownumb > 0) {
            return rownumb;
        } else {
            return 0;
        }
    }

    /**
     * 查询风险配置信息
     * created by jiangjx on 2016年12月30日 13:56:16

     * @param riskId 风险id
     * @return
     */
    @Override
    public SecurityRiskRescVo queryRiskInfoById(String riskId) throws Exception {
        SecurityRiskRescVo returnValue = new SecurityRiskRescVo();
        Map<String, String> baseVal = securityRiskRescMapper.queryRiskInfoById(riskId);//根据ID查询风险配置详细信息
        returnValue.setDescription(baseVal.get("DESCRIPTION"));//set描述
        returnValue.setSecyrityLevel(baseVal.get("SECYRITY_LEVEL"));//set风险等级
        returnValue.setWorkcellinfoname(baseVal.get("NAME"));//set操作单元名称
        returnValue.setUnitId(baseVal.get("UNIT_ID"));//set操作单元ID
        returnValue.setRiskId(baseVal.get("SECURITY_RISK_ID"));//set风险配置主键ID
        return returnValue;
    }

    /**
     *
     * @param  version
     * @return 版本复制 ； 将当前版本的复制进行修改
     * @throws Exception
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Map<String, Object> cloneRiskDesc(Version version, boolean isWorkCell, Employee employee) throws Exception {
        Map<String,Object> resMap = new HashMap<>();
        /**
         * 判断是否是操作单元
         * 或者 一站一案 的风险配置
         */
        SecurityRiskResc securityRiskResc = new SecurityRiskResc();
        securityRiskResc.setUnitId(version.getRelatedId());
        // true 操作单元
        if(isWorkCell){
        WorkCellInfo workCellInfo =    jsmWorkCellInfoMapper.selectByPrimaryKey(version.getRelatedId());
            securityRiskResc.setVersion(workCellInfo.getStepVersion());
        }else{
            // 一站一案问题
            WorkCellInstanceVo workCellInstanceVo = jsmWorkCellInstanceMapper.selectByPrimaryKey(version.getRelatedId());
            securityRiskResc.setVersion(workCellInstanceVo.getStepVersion());
        }
        List<SecurityRiskResc> securityRiskRescList = securityRiskRescMapper.queryTableList(securityRiskResc);
        if(securityRiskRescList!=null && securityRiskRescList.size() > 0){
            String creatorId = employee.getEmployeeId();
            for(SecurityRiskResc sr :  securityRiskRescList){
                sr.setCreatorId(creatorId);
                sr.setModifierId(creatorId);
                sr.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                sr.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                if(sr.getVersion()!=null) {
                    sr.setSecurityRiskId(UniqueUtil.uuid());
                    sr.setVersion(version.getVersionNumber());
                    securityRiskRescMapper.insertSelective(sr);
                }else{
                    sr.setVersion(version.getVersionNumber());
                    securityRiskRescMapper.updateByPrimaryKeySelective(sr);
                }
            }
        }
        return resMap ;
    }
}
